package cn.bird.exception;

import lombok.Getter;

import java.util.Objects;
import java.util.Optional;

/**
 * 业务异常处理
 * @author xuyunpeng
 */
@Getter
public class BusiException extends RuntimeException{
    private final String code;
    private final String msg;
    private final Object object;

    public BusiException(String code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
        this.object = null;
    }

    public BusiException(String msg){
        super(msg);
        this.code = CoreStatusInfo.BUSINESS_ERROR.getCode();
        this.msg = msg;
        this.object = null;
    }

    public BusiException(IStatus status){
        super(Optional.ofNullable(status).map(IStatus::getMessage).orElse(CoreStatusInfo.BUSINESS_ERROR.getMessage()));
        if (Objects.isNull(status)) {
            this.code = CoreStatusInfo.BUSINESS_ERROR.getCode();
            this.msg = CoreStatusInfo.BUSINESS_ERROR.getMessage();
        }else {
            this.code = status.getCode();
            this.msg = status.getMessage();
        }
        this.object = null;
    }


    public BusiException(IStatus status, Object object){
        super(Optional.ofNullable(status).map(IStatus::getMessage).orElse(CoreStatusInfo.BUSINESS_ERROR.getMessage()));
        if (Objects.isNull(status)) {
            this.code = CoreStatusInfo.BUSINESS_ERROR.getCode();
            this.msg = CoreStatusInfo.BUSINESS_ERROR.getMessage();
        }else {
            this.code = status.getCode();
            this.msg = status.getMessage();
        }
        this.object = object;
    }
}
